<?php
session_start();
include_once("config/config.inc.php");
include_once("monthly.class.php");
include_once("acctg/class.baseobject.php");
include_once("acctg/class.shift.php");

$lsql = "select settings_value from settings where id = '3'";
$lres = mysql_query($lsql);
list($lobbyid)=mysql_fetch_row($lres);


function getCoopReport($month,$year)
{
	$num = cal_days_in_month(CAL_GREGORIAN, $month, $year) ;

	$sql = "select settings_value from settings where id = '1'";
	$res = mysql_query($sql);
	list($value) = mysql_fetch_row($res);

	$ret = "<div style='font-weight:bold'>".strtoupper($value)."<br>
	COOP SALES SUMMARY<br>
	FOR THE MONTH OF ".strtoupper(getMonthName($month))." ".$year."</div><br><br>";
	$ret .= "<table border=1 cellpadding=3 cellspacing=0>";
	$ret .= "<tr>";
	$ret .= "<th>&nbsp;</th>";
	$ret .= "<th>&nbsp;</th>";
	$ret .= "<th>1st shift</th>";
	$ret .= "<th>2nd shift</th>";
	$ret .= "<th>3rd shift</th>";
	$ret .= "<th>Total</th>";
	$ret .= "</tr>";
	for($i = 1;  $i <= $num; $i++)
	{
		$start = date('Y-m-d H:i:s', strtotime('-1800 seconds',strtotime($year."-".$month."-".$i." 00:00:00")));
		$end = date('Y-m-d H:i:s', strtotime('+1800 seconds',strtotime($year."-".$month."-".$i." 23:59:59")));

		$sql = "select  `datetime` from `shift-transactions` where `datetime` between '$start' and '$end' 
		and shift = 'start' order by `datetime` asc limit 0,1";
		$res = mysql_query($sql);
		list($startdt1)=mysql_fetch_row($res);
		
		$sql = "select  `datetime` from `shift-transactions` where `datetime` between '$start' and '$end' 
		and shift = 'end' and `datetime` > '$startdt1' order by `datetime` asc limit 0,1";
		$res = mysql_query($sql);
		list($enddt1)=mysql_fetch_row($res);

		$sql = "select  `datetime` from `shift-transactions` where `datetime` between '$start' and '$end' 
		and shift = 'start' order by `datetime` asc limit 1,1";
		$res = mysql_query($sql);
		list($startdt2)=mysql_fetch_row($res);
		
		$sql = "select  `datetime` from `shift-transactions` where `datetime` between '$start' and '$end' 
		and shift = 'end' and `datetime` > '$startdt2' order by `datetime` asc limit 0,1";
		$res = mysql_query($sql);
		list($enddt2)=mysql_fetch_row($res);

		$sql = "select  `datetime` from `shift-transactions` where `datetime` between '$start' and '$end' 
		and shift = 'start' order by `datetime` asc limit 2,1";
		$res = mysql_query($sql);
		list($startdt3)=mysql_fetch_row($res);
		
		$sql = "select  `datetime` from `shift-transactions` where `datetime` between '$start' and '$end' 
		and shift = 'end' and `datetime` > '$startdt3' order by `datetime` asc limit 0,1";
		$res = mysql_query($sql);
		list($enddt3)=mysql_fetch_row($res);

		/*
		$shift1 =  new monthly('',$startdt1,$enddt1);
		$shift2 =  new monthly('',$startdt2,$enddt2);
		$shift3 =  new monthly('',$startdt3,$enddt3);
		*/

		$today = $year.'-'.$month.'-'.$i;
		$shiftTime1 = new shift(array('date'=>$today,'shiftno'=>1));
		$shiftTime2 = new shift(array('date'=>$today,'shiftno'=>2));
		$shiftTime3 = new shift(array('date'=>$today,'shiftno'=>3));

		$shiftTime1->getShiftDuration();
		$shiftTime2->getShiftDuration();
		$shiftTime3->getShiftDuration();


		$shift1 =  new monthly('',$shiftTime1->shiftStart,$shiftTime1->shiftEnd);
		$shift2 =  new monthly('',$shiftTime2->shiftStart,$shiftTime2->shiftEnd);
		$shift3 =  new monthly('',$shiftTime3->shiftStart,$shiftTime3->shiftEnd);

		$coopsales1 = $shift1->getCoopSales();
		$coopsales2 = $shift2->getCoopSales();
		$coopsales3 = $shift3->getCoopSales();

		$total = $coopsales1+$coopsales2+$coopsales3;

		$ret .= "<tr>";
		$ret .= "<td>$i</td>";
		$ret .= "<td>&nbsp;</td>";
		$ret .= "<td style='text-align:right'>".number_format($coopsales1)."</td>";
		$ret .= "<td style='text-align:right'>".number_format($coopsales2)."</td>";
		$ret .= "<td style='text-align:right'>".number_format($coopsales3)."</td>";
		$ret .= "<td style='text-align:right'>".number_format($total)."</td>";
		$ret .= "</tr>";
		
		$_coopsales1 += $coopsales1;
		$_coopsales2 += $coopsales1;
		$_coopsales3 += $coopsales1;
		$_total += $total;
	}	
	$ret .= "<tr>";
	$ret .= "<td>&nbsp;</td>";
	$ret .= "<td>&nbsp;</td>";
	$ret .= "<th style='text-align:right'>".number_format($_coopsales1)."</th>";
	$ret .= "<th style='text-align:right'>".number_format($_coopsales2)."</th>";
	$ret .= "<th style='text-align:right'>".number_format($_coopsales3)."</th>";
	$ret .= "<th style='text-align:right'>".number_format($_total)."</th>";
	$ret .= "</tr>";
	$ret .= "</table>";

	$ret .= "<br><br>";
	$ret .= "This are the cooperative sales";
	$ret .= "<br>";
	$sql = "SELECT sas_id,sas_description FROM `sales_and_services` where sas_cat_id = '2' order by sas_description";
	$res = mysql_query($sql);
	$ret .= "<table>";

	$start = date('Y-m-d H:i:s', strtotime($year."-".$month."-1 00:00:00"));
	$end = date('Y-m-d H:i:s', strtotime($year."-".$month."-".$num." 23:59:59"));

	while(list($sas_id,$sas)=mysql_fetch_row($res))
	{
		$sastot = 0;
		$_sql = "select sum(unit_cost*qty) from room_sales where item_id = '$sas_id' and status = 'Paid' and sales_date between '$start' and '$end' ";
		$_res = mysql_query($_sql);
		/*while(list($sasamount,$sasdate)=mysql_fetch_row($_res))
		{
			$sastot += $sasamount;
		}
		*/
		list($sastot)=mysql_fetch_row($_res);
		$ret .= "<tr>";
		$ret .= "<td>$sas</td>";
		$ret .= "<td>&nbsp;</td>";
		$ret .= "<td>";
		$ret .= (is_null($sastot)) ? 0 : $sastot;
		$ret .="</td>";
		$ret .= "</tr>";
	}
	$ret .= "</table>";
	return $ret;
}

function getMonthDropdown($name="month", $selected=null)
	{
			$dd = '<select name="'.$name.'" id="'.$name.'">';

			$months = array(
					1 => 'January',
					2 => 'February',
					3 => 'March',
					4 => 'April',
					5 => 'May',
					6 => 'June',
					7 => 'July',
					8 => 'August',
					9 => 'September',
					10 => 'October',
					11 => 'November',
					12 => 'December');
			/*** the current month ***/
			$selected = is_null($selected) ? date('n', time()) : $selected;

			for ($i = 1; $i <= 12; $i++)
			{
					$dd .= '<option value="'.$i.'"';
					if ($i == $selected)
					{
							$dd .= ' selected';
					}
					/*** get the month ***/
					$dd .= '>'.$months[$i].'</option>';
			}
			$dd .= '</select>';
			return $dd;
	}

	function getYearDropdown($name="year", $selected=null)
	{
			$dd = '<select name="'.$name.'" id="'.$name.'">';

			$months = array(
					1 => '2009',
					2 => '2010',
					3 => '2011',
					4 => '2012',
					5 => '2013',
					6 => '2014',
					7 => '2015',
					8 => '2016');
		   
			$selected = is_null($selected) ? date('Y', time()) : $selected;

			for ($i = 1; $i <= 8; $i++)
			{
					$dd .= '<option value="'.$months[$i].'"';
					if ($months[$i] == $selected)
					{
							$dd .= ' selected';
					}
					
					$dd .= '>'.$months[$i].'</option>';
			}
			$dd .= '</select>';
			return $dd;
	}
	function getMonthName($i)
	{
		$months = array(
					1 => 'January',
					2 => 'February',
					3 => 'March',
					4 => 'April',
					5 => 'May',
					6 => 'June',
					7 => 'July',
					8 => 'August',
					9 => 'September',
					10 => 'October',
					11 => 'November',
					12 => 'December');
		return $months[$i];
	}
?>
<style>
		.printable {
			font-family: sans-serif;
			font-size: 14px;
			font-weight: 550;
			}
		.report{
			font-family: sans-serif;
			font-size: 14px ;
			text-align:left;
			font-weight: 550;
				
		}
		.report td{
			padding-bottom:12px;
		}
		.summary{
			font-family: sans-serif;
			font-size: 14px;
			text-align:left;
			font-weight: 550;
		}
</style>
<script src="../js/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="../js/jquery.print.js"></script>
<script type="text/javascript">
 
		// When the document is ready, initialize the link so
		// that when it is clicked, the printable area of the
		// page will print.
		$(document).ready(function(){		
				$("a").attr( "href", "javascript:void( 0 )" ).click(
						function(){
							// Print the DIV.
							$(".printable2").print();
							$(".printable").print(); 							
							// Cancel click event.
							return( false );
						});
 
			
		});
 
</script>
<form name=myform method=post>
<div>
Month: <? echo getMonthDropdown("ddlmonth",$_POST["ddlmonth"]); ?>
<br>
<br>
Year: <? echo getYearDropdown("ddlyear",$_POST["ddlyear"]); ?>
</div>
<br>
<input type='submit' value='Search' name='btnSearch' />
<br>
<br>
<a href="#">Print Coop Report</a>
<br>
<br>
<div class='printable'>
<? if($_POST){ echo getCoopReport($_POST["ddlmonth"],$_POST["ddlyear"]);} ?>
</div><br />
<a href="#">Print Report</a>
</form>